import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:example/config/color_config.dart';
import 'package:example/utils/common.dart';
import 'package:example/widget/primary_btn.dart';

class ConfirmDialog extends StatefulWidget {
  String? title;
  String? content;
  Color? contentTextColor = ColorConfig.FF333333;
  String? confirmText;
  Color? confirmTextColor = ColorConfig.main;
  String? cancelText;
  bool? hideCancel;
  Function(BuildContext context)? confirm;
  Function(BuildContext context)? cancel;

  ConfirmDialog({
    Key? key,
    this.title,
    this.content,
    this.contentTextColor,
    this.confirmText,
    this.confirmTextColor,
    this.cancelText,
    this.hideCancel,
    this.confirm,
    this.cancel,
  }) : super(key: key);

  @override
  State<ConfirmDialog> createState() => _ConfirmDialogState();
}

class _ConfirmDialogState extends State<ConfirmDialog> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: 287.w,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: _body(context),
      ),
    );
  }

  _body(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        _title(),
        _content(),
        _bottomLayout(context),
      ],
    );
  }

  _title() {
    return Visibility(
      visible: isNotEmptyOrNull(widget.title),
      child: Container(
        padding: EdgeInsets.only(top: 22.w),
        child: Text(
          widget.title ?? "",
          textAlign: TextAlign.center,
          style: TextStyle(
            color: Colors.black,
            fontSize: 18.sp,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }

  _content() {
    return Container(
      padding: EdgeInsets.fromLTRB(
          14.w, isNotEmptyOrNull(widget.title) ? 18.w : 35.w, 14.w, 32.w),
      alignment: Alignment.center,
      child: Text(
        widget.content ?? "",
        maxLines: 4,
        overflow: TextOverflow.ellipsis,
        textAlign: TextAlign.center,
        style: TextStyle(
          color: widget.contentTextColor,
          fontSize: 16.sp,
        ),
      ),
    );
  }

  _bottomLayout(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Container(
            color: ColorConfig.FFD8D8D8,
            width: double.infinity,
            height: 0.5.w,
          ),
          Row(
            children: [
              Visibility(
                visible: !(widget.hideCancel ?? false),
                child: Expanded(
                  child: PrimaryBtn(
                    height: 48.w,
                    borderRadius:
                    BorderRadius.only(bottomLeft: Radius.circular(12.r)),
                    text: widget.cancelText ?? "取消",
                    textColor: const Color(0xFF191919),
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w400,
                    onTap: () {
                      widget.cancel?.call(context);
                    },
                  ),
                ),
              ),
              Visibility(
                visible: !(widget.hideCancel ?? false),
                child: Container(
                  color: ColorConfig.FFD8D8D8,
                  height: 48.w,
                  width: 0.5.w,
                ),
              ),
              Expanded(
                child: PrimaryBtn(
                  height: 48.w,
                  borderRadius: !(widget.hideCancel ?? false)
                      ? BorderRadius.only(bottomRight: Radius.circular(12.r))
                      : BorderRadius.vertical(bottom: Radius.circular(12.r)),
                  text: widget.confirmText ?? "确定",
                  textColor: widget.confirmTextColor,
                  fontSize: 16.sp,
                  onTap: () {
                    widget.confirm?.call(context);
                  },
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

Future<bool?> showCenterDialog(
    BuildContext context, {
      String? title,
      String? content,
      Color? contentTextColor = ColorConfig.FF333333,
      String? confirmText,
      Color? confirmTextColor = ColorConfig.main,
      String? cancelText,
      bool? hideCancel,
      bool? barrierDismissible,
      Function(BuildContext context)? confirm,
      Function(BuildContext context)? cancel,
    }) {
  return showGeneralDialog(
    context: context,
    barrierDismissible: barrierDismissible ?? true, ///点击边缘消失
    barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
    transitionDuration: const Duration(milliseconds: 200),
    pageBuilder: (BuildContext buildContext, Animation<double> animation,
        Animation<double> secondaryAnimation) {
      return Builder(
        builder: (BuildContext context) {
          return Material(
              type: MaterialType.transparency, ///以确保对话框背景是透明的。
              child: ConfirmDialog(
                  title: title,
                  content: content,
                  confirmText: confirmText,
                  confirmTextColor: confirmTextColor,
                  cancelText: cancelText,
                  hideCancel: hideCancel,
                  confirm: confirm,
                  cancel: cancel,
                  contentTextColor: contentTextColor));
        },
      );
    },
  );
}
